﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_ThemMoiDoanVien : System.Web.UI.Page
{
    readonly _dbDataContext _db = new _dbDataContext();
    readonly SecurityLib.Sec_AES _se = new SecurityLib.Sec_AES();
    public string st = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            CatalogAccess.BindItemToDropDown(drldantoc,1);
            drldantoc.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drltongiao,  2);
            drltongiao.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drlHonnhan,  3);
            drlHonnhan.SelectedIndex = 1;
            CatalogAccess.BindItemToDropDown(drlKhuVuc, 1015);
            BindItemToTrinhDoVanHoa();
            CatalogAccess.BindItemToDropDown(drlNgoaingu,  4);
            CatalogAccess.BindItemToDropDown(drlTinhoc,  5);
            CatalogAccess.BindItemToDropDown(drlChucVuDoan, 9);
            drlChucVuDoan.SelectedIndex = 6;
            CatalogAccess.BindItemToDropDown(drlTrinhdochuyenmon, 1016);
            BindItemToSucKhoe();
            string madonvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);
            txtMaDonVien.Text = string.Format("{0:00000000}", CatalogAccess.GetLastMaDoanVienCuaDonVi(madonvi));
            st = _se.Decrypt(Request.QueryString["tid"].Replace(" ","+"),"gold",128);
        }
    }
    //Add item to drlTrinhDo
    private void BindItemToTrinhDoVanHoa()
    {
        drlTrinhdovanhoa.Items.Clear();
        ListItem item=new ListItem("---Chọn trình độ---","-1");
        drlTrinhdovanhoa.Items.Add(item);
        for (int i = 1; i <= 12; i++)
        {
            item = new ListItem(i.ToString(), i.ToString());
            drlTrinhdovanhoa.Items.Add(item);
        }
        item = new ListItem("Chưa đi học", "13");
        drlTrinhdovanhoa.Items.Add(item);
    }
    
    //Add item to drlSuckhoe
    private void BindItemToSucKhoe()
    {
        drlSuckhoe.Items.Clear();
        ListItem item;
        item = new ListItem("Tốt","1");
        drlSuckhoe.Items.Add(item);
        item = new ListItem("Bình thường", "2");
        drlSuckhoe.Items.Add(item);
        item = new ListItem("Không tốt", "3");
        drlSuckhoe.Items.Add(item);
    }

    
    protected void rdbMoi_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbMoi.Checked == true)
        {
            lblngaychuyen.Visible = false;
            lbldonvicu.Visible = false;
            txtngaychuyenden.Visible = false;
            txtDonViCu.Visible = false;
            Image4.Visible = false;
        }
    }
    protected void rdbchuyentoi_CheckedChanged(object sender, EventArgs e)
    {
        if (rdbchuyentoi.Checked == true)
        {
            lblngaychuyen.Visible = true;
            lbldonvicu.Visible = true;
            txtngaychuyenden.Visible = true;
            txtDonViCu.Visible = true;
            Image4.Visible = true;
        }
    }

    private List<TextBox> GetTextBoxControls(Control ctrl)
    {
        var uCtrls = new List<TextBox>();
        foreach (Control child in ctrl.Controls)
        {
            if (child is TextBox) uCtrls.Add((TextBox)child);
            uCtrls.AddRange(GetTextBoxControls(child));
        }
        return uCtrls;
    }
    private void ClearDataInControlForNew()
    {
        string madonvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);
        CatalogAccess.BindItemToDropDown(drldantoc, 1);
        drldantoc.SelectedIndex = 1;
        CatalogAccess.BindItemToDropDown(drltongiao, 2);
        drltongiao.SelectedIndex = 1;
        CatalogAccess.BindItemToDropDown(drlHonnhan, 3);
        drlHonnhan.SelectedIndex = 1;
        CatalogAccess.BindItemToDropDown(drlKhuVuc, 1015);
        BindItemToTrinhDoVanHoa();
        CatalogAccess.BindItemToDropDown(drlNgoaingu, 4);
        CatalogAccess.BindItemToDropDown(drlTinhoc, 5);
        CatalogAccess.BindItemToDropDown(drlTrinhdochuyenmon, 1016);
        CatalogAccess.BindItemToDropDown(drlChucVuDoan, 9);
        drlChucVuDoan.SelectedIndex = 6;
        BindItemToSucKhoe();
        var listOfUserControls = GetTextBoxControls(Page);
        foreach (TextBox ctrl in listOfUserControls)
        {
            ctrl.Text = "";
        }
        txtMaDonVien.Text = string.Format("{0:00000000}", CatalogAccess.GetLastMaDoanVienCuaDonVi(madonvi));
        StatusLabel.Visible = false;
    }

   //them doan vien du bi
    protected void btnsavedubi_Click(object sender, EventArgs e)
    {
        string madonvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);
        string filehinh = "";
        if (imgdoanvien.HasFile)
        {
            filehinh = "img" + txtMaDonVien.Text.Trim() + Path.GetExtension(imgdoanvien.FileName);
        }
        string strmadoanvien = "";
        try { strmadoanvien = madonvi.Trim().Substring(4) + txtMaDonVien.Text; }
        catch { strmadoanvien = txtMaDonVien.Text; }
        bool success = CatalogAccess.InsertDoanVien(strmadoanvien, filehinh, txtHoTen.Text, nam.Checked, txtngaysinh.Text,
            txtnoisinh.Text, txtCMND.Text, txtngaycapcmnd.Text, txtNoiCap.Text, txtNguyenquan.Text, txtThuongTru.Text, txtHienNay.Text, txtDienthoai.Text,
            txtEmail.Text, int.Parse(drldantoc.SelectedValue.ToString()), int.Parse(drltongiao.SelectedValue.ToString()), int.Parse(drlHonnhan.SelectedValue.ToString()),
            txtXuatthan.Text, txtNghenghiep.Text, txtMota.Text, drlTrinhdovanhoa.SelectedItem.Text, int.Parse(drlTrinhdochuyenmon.SelectedValue.ToString()), txtbang1.Text + "@"+ txtbang2.Text,
            int.Parse(drlNgoaingu.SelectedValue.ToString()), int.Parse(drlTinhoc.SelectedValue.ToString()), drlSuckhoe.SelectedItem.Text, txtngayvaodoan.Text, (rdbchuyentoi.Checked == true ? true : false),
            txtngaychuyenden.Text, txtDonViCu.Text, madonvi, 2, int.Parse(drlKhuVuc.SelectedValue.ToString()), chk_dangvien.Checked,txtchucvunoicongtac.Text,int.Parse(drlChucVuDoan.SelectedValue.ToString()));
        if (success)
        {
            //luu hinh len server
            if (filehinh != "")
            {
                try
                {
                    imgdoanvien.SaveAs(Server.MapPath(PageURL.local + "imgdoanvien/" + filehinh));
                }
                catch { }
            }
            //them doan vien nay vao bang thong tin khac cua doan vien
            tblThongTinKhac _table = new tblThongTinKhac();
            _table.MaDoanVien = strmadoanvien;
            try
            {
                _db.tblThongTinKhacs.InsertOnSubmit(_table);
                _db.SubmitChanges();
            }
            catch { }
            //them doan vien nay vao kinh te gia dinh
            tblKinhTeGiaDinh _kinhte = new tblKinhTeGiaDinh();
            _kinhte.MaDoanVien = strmadoanvien;
            _kinhte.LoaiNhaThueCap = -1;
            _kinhte.LoaiNhaXayMua = -1;
            _kinhte.NhaCap = false;
            _kinhte.NhaXay = false;
            _kinhte.LuongNam = "";
            _kinhte.NguonKhac = "";
            _kinhte.QuanTrinhLuong = "";
            _kinhte.DienTichThueCap = "";
            _kinhte.TongDienTichXayMua = "";
            _kinhte.DatSanXuatKinhDoanh = "";
            _kinhte.DatTuMua = "";
            _kinhte.DatDuocCap = "";
            try
            {
                _db.tblKinhTeGiaDinhs.InsertOnSubmit(_kinhte);
                _db.SubmitChanges();
            }
            catch { }
            //them doan vien nay vao bang cong tac doan
            //tblCongTacDoan _tblcongtacdoan = new tblCongTacDoan();
            //_tblcongtacdoan.MaDoanVien = txtMaDonVien.Text;
            //try
            //{
            //    _db.tblCongTacDoans.InsertOnSubmit(_tblcongtacdoan);
            //    _db.SubmitChanges();
            //}
            //catch { }
            ClearDataInControlForNew();
            SecurityLib.ToolsMore.ShowPopUpMsg("Đã thêm thành công đoàn viên dự bị",this.Page);
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Không thêm được đoàn viên.", this.Page);
        }
    }
    //them doan vien chinh thuc
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string madonvi = _se.Decrypt(Request.QueryString["dvid"].Replace(" ", "+"), "gold", 128);
        string filehinh = "";
        if (imgdoanvien.HasFile)
        {
            filehinh = "img" + txtMaDonVien.Text.Trim() + Path.GetExtension(imgdoanvien.FileName);
        }
        string strmadoanvien = "";
        try { strmadoanvien = madonvi.Trim().Substring(4) + txtMaDonVien.Text; }
        catch { strmadoanvien = txtMaDonVien.Text; }
        bool success = CatalogAccess.InsertDoanVien(strmadoanvien, filehinh, txtHoTen.Text, nam.Checked ,txtngaysinh.Text,
            txtnoisinh.Text,txtCMND.Text,txtngaycapcmnd.Text,txtNoiCap.Text,txtNguyenquan.Text,txtThuongTru.Text,txtHienNay.Text,txtDienthoai.Text,
            txtEmail.Text, int.Parse(drldantoc.SelectedValue.ToString()), int.Parse(drltongiao.SelectedValue.ToString()), int.Parse(drlHonnhan.SelectedValue.ToString()),
            txtXuatthan.Text, txtNghenghiep.Text, txtMota.Text, drlTrinhdovanhoa.SelectedItem.Text, int.Parse(drlTrinhdochuyenmon.SelectedValue.ToString()),txtbang1.Text+"@"+txtbang2.Text,
            int.Parse(drlNgoaingu.SelectedValue.ToString()),int.Parse(drlTinhoc.SelectedValue.ToString()),drlSuckhoe.SelectedItem.Text,txtngayvaodoan.Text,(rdbchuyentoi.Checked==true?true:false),
            txtngaychuyenden.Text, txtDonViCu.Text, madonvi ,1,int.Parse(drlKhuVuc.SelectedValue.ToString()),chk_dangvien.Checked,txtchucvunoicongtac.Text,int.Parse(drlChucVuDoan.SelectedValue.ToString()));
        if (success)
        {
            //luu hinh len server
            if (filehinh != "")
            {
                try
                {
                    imgdoanvien.SaveAs(Server.MapPath(PageURL.local + "imgdoanvien/" + filehinh));
                }
                catch { }
            }
            //them doan vien nay vao bang thong tin khac cua doan vien
            tblThongTinKhac _table = new tblThongTinKhac();
            _table.MaDoanVien = strmadoanvien;
            try
            {
                _db.tblThongTinKhacs.InsertOnSubmit(_table);
                _db.SubmitChanges();
            }
            catch { }
            //them doan vien nay vao kinh te gia dinh
            tblKinhTeGiaDinh _kinhte = new tblKinhTeGiaDinh();
            _kinhte.MaDoanVien = strmadoanvien;
            _kinhte.LoaiNhaThueCap = -1;
            _kinhte.LoaiNhaXayMua = -1;
            _kinhte.NhaCap = false;
            _kinhte.NhaXay = false;
            _kinhte.LuongNam = "";
            _kinhte.NguonKhac = "";
            _kinhte.QuanTrinhLuong = "";
            _kinhte.DienTichThueCap = "";
            _kinhte.TongDienTichXayMua = "";
            _kinhte.DatSanXuatKinhDoanh = "";
            _kinhte.DatTuMua = "";
            _kinhte.DatDuocCap = "";
            try
            {
                _db.tblKinhTeGiaDinhs.InsertOnSubmit(_kinhte);
                _db.SubmitChanges();
            }
            catch { }
            //them doan vien nay vao bang cong tac doan
            //tblCongTacDoan _tblcongtacdoan = new tblCongTacDoan();
            //_tblcongtacdoan.MaDoanVien = strmadoanvien + txtMaDonVien.Text;
            //try
            //{
            //    _db.tblCongTacDoans.InsertOnSubmit(_tblcongtacdoan);
            //    _db.SubmitChanges();
            //}
            //catch { }
            ClearDataInControlForNew();
            SecurityLib.ToolsMore.ShowPopUpMsg("Đã thêm thành công đoàn viên.", this.Page);
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Không thêm được đoàn viên.", this.Page);
        }
    }
}